<template>
	<view class="normal-login-container">
		<view class="logo-content1 align-center justify-center flex" v-show="usershow1">
			<view class="uni-flex uni-row">
				<view class="flex-item" style="margin-right: 23%;">
					<image style="width: 350px;height: 100px;" :src="globalConfig.appInfo.logo" />
				</view>
				<view class="flex-item title"><text >用户登录</text></view>
			</view>
		</view>
		<view v-show="usershow2">
			<view class="logo-content align-center justify-center flex">
				<image style="width: 250px;height: 85px;margin-right: 10%;" :src="globalConfig.appInfo.logo" />
				<!-- <text  class="title">用户登录</text> -->
			</view>

			<view style="font-size: 20px;text-align: center;margin-top:2%;">
				沈阳富创MES系统
			</view>
			<view style="font-size: 15px;text-align: center;">
				故障维修系统
			</view>
		</view>
		<view class="login-form-content">
			<view class="input-item flex align-center">
				<view class="iconfont icon-user icon"></view>
				<input v-model="loginForm.userName" class="input" type="text" placeholder="请输入账号" maxlength="30" />
			</view>
			<view class="input-item flex align-center">
				<view class="iconfont icon-password icon"></view>
				<input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
			</view>
			<view class="action-btn">
				<button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		Login
	} from '@/api/login'
	import md5 from "@/api/md5";
	export default {
		data() {
			return {
				usershow1: true,
				usershow2: false,
				codeUrl: "",
				captchaEnabled: true,
				// 用户注册开关
				register: false,
				globalConfig: getApp().globalData.config,
				loginFormVar:{},
				loginForm: {
					"browserIp": this.getDeviceIp(),
					"userName": uni.getStorageSync('loginFormVar').userName,
					"password": uni.getStorageSync('loginFormVar').password,
					"status": false
				}
			}
		},
		created() {
			var screenWidth = uni.getWindowInfo().screenWidth
			if (screenWidth < 900) {
				this.usershow1 = false
				this.usershow2 = true
			}
		},
		methods: {
			// getPhoneModel() {
			// 	let brand = '';
			// 	let zhDev = {
			// 		'apple': 'iPhone',
			// 		'samsung': '三星',
			// 		'huawei': '华为',
			// 		'xiaomi': '小米',
			// 		'oppo': 'OPPO',
			// 		'vivo': 'Vivo',
			// 		'oneplus': '一加',
			// 		'sony': '索尼',
			// 		'lg': 'LG',
			// 		'motorola': '摩托罗拉',
			// 		'google': '谷歌',
			// 		'nokia': '诺基亚',
			// 		'htc': 'HTC',
			// 		'lenovo': '联想',
			// 		'asus': '华硕',
			// 		'zte': '中兴',
			// 		'meizu': '魅族',
			// 		'coolpad': '酷派',
			// 	}
			// 	if (uni.getSystemInfoSync().platform === 'android' || 'ios') {
			// 		let model = uni.getSystemInfoSync().brand.toLowerCase();
			// 			console.log('手机品牌1', model)
			// 		brand = zhDev[model] ? zhDev[model] : '其它'
			// 	} else {
			// 		brand = '其它'
			// 	}
			// 	console.log('手机品牌2', brand)
			// },
			// 登录方法
			async handleLogin() {
				//this.getPhoneModel()
				if (this.loginForm.username === "") {
					this.$modal.msgError("请输入您的账号")
				} else if (this.loginForm.password === "") {
					this.$modal.msgError("请输入您的密码")
					// } else if (this.loginForm.code === "" && this.captchaEnabled) {
					//   this.$modal.msgError("请输入验证码")
				} else {
					this.$modal.loading("登录中，请耐心等待...")
					this.pwdLogin()
				}
			},
			//获取Ip地址  
			getDeviceIp() {
				var deviceIp = ''
				if (plus.os.name == "Android") {
					var Context = plus.android.importClass("android.content.Context");
					var wifiManager = plus.android.runtimeMainActivity().getSystemService(Context.WIFI_SERVICE);
					var wifiInfo = plus.android.invoke(wifiManager, "getConnectionInfo");
					var ipAddress = plus.android.invoke(wifiInfo, "getIpAddress");
					deviceIp = '';
					if (ipAddress != 0) {
						deviceIp = ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "." + (ipAddress >> 16 & 0xff) +
							"." + (ipAddress >> 24 & 0xff));
					}
				}
			//	console.log(deviceIp)
				return deviceIp;
			},
			//获取设备的mac地址
			getDeviceMac() {
				//获取本机Mac地址
				var deviceMac = '';
				var net = plus.android.importClass('java.net.NetworkInterface');
				var wl0 = net.getByName('wlan0');
				var macByte = wl0.getHardwareAddress();
				//console.log(macByte)
				deviceMac = '';
				for (var i = 0; i < macByte.length; i++) {
					var tmp = '';
					var num = macByte[i];
					if (num < 0) {
						tmp = (255 + num + 1).toString(16);
					} else {
						tmp = num.toString(16);
					}
					if (tmp.length == 1) {
						tmp = '0' + tmp;
					}
					deviceMac += tmp;
				}
				console.log(deviceMac);
			},
			// 密码登录
			async pwdLogin() {
				this.loginFormVar = {
					"browserIp": this.loginForm.browserIp,
					"userName": this.loginForm.userName,
					"password": this.loginForm.password,
					"status": false
				}
				let login = {
					"browserIp": this.loginForm.browserIp,
					"userName": this.loginForm.userName,
					"password": md5.hex_md5(this.loginForm.password),
					"status": false
				}
				this.$store.dispatch('Login', login).then(() => {
					this.$modal.closeLoading()
					this.loginSuccess()
					this.getDeviceMac()
				}).catch(() => {
					if (this.captchaEnabled) {
						// this.getCode()
					}
				})
			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				try {
					uni.setStorageSync('loginFormVar', this.loginFormVar);
				} catch (e) {
					this.$modal.msgError("写入缓存密码错误"+e)
				}
				var screenWidth = uni.getWindowInfo().screenWidth
				if (screenWidth < 900) {
					uni.navigateTo({
						url: '/pages/pda/index'
					})
				} else {
					this.$tab.reLaunch('/pages/home')
				}

				// 设置用户信息
				// this.$store.dispatch('GetInfo').then(res => {
				// 
				// })
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.normal-login-container {
		width: 100%;
        .logo-content1 {
			width: 100%;
			font-size: 21px;
			text-align: center;
			padding-top: 1%;

			image {
				border-radius: 4px;
			}

			.title {
				margin-top: 10%;
				margin-bottom: 10%;
			}
		}

		.logo-content {
			width: 100%;
			font-size: 21px;
			text-align: center;
			padding-top: 10%;

			image {
				border-radius: 4px;
			}

		}

		.login-form-content {
			text-align: center;
			margin: 20px auto;
			width: 80%;

			.input-item {
				margin: 20px auto;
				background-color: #f5f6f7;
				height: 45px;
				border-radius: 20px;

				.icon {
					font-size: 38rpx;
					margin-left: 10px;
					color: #999;
				}

				.input {
					width: 100%;
					font-size: 14px;
					line-height: 20px;
					text-align: left;
					padding-left: 15px;
				}

			}

			.login-btn {
				margin-top: 40px;
				height: 45px;
			}

			.reg {
				margin-top: 15px;
			}

			.xieyi {
				color: #333;
				margin-top: 20px;
			}

			.login-code {
				height: 38px;
				float: right;

				.login-code-img {
					height: 38px;
					position: absolute;
					margin-left: 10px;
					width: 200rpx;
				}
			}
		}
	}
</style>